package com.mizhiquan.util;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;

/**
 * Created SUDDEV on 2017/5/19 0019.
 * Date与String相互转换工具类
 */
public class DateTimeUtil {

    public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";

    //joda-time
    //str->date
    public static Date str2Date(String dateTimeStr){
        DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern(STANDARD_FORMAT);
        DateTime dateTime= dateTimeFormat.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }
    //date->str
    public static String date2Str(Date date){
        if (date==null){
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(STANDARD_FORMAT);
    }
    //获取当前时间
    public static Date getNowTime(){
        return new Date();
    }
}
